import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

// import { h } from 'vue';
import { DictEnum } from '@vben/constants';
// import { VbenIcon } from '@vben/icons';
import { getPopupContainer } from '@vben/utils';

// import { Tag } from 'ant-design-vue';
// import { z } from '#/adapter/form';
import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Select',
    componentProps: {
      getPopupContainer,
      options: getDictOptions(DictEnum.PERSONAL_TAG_COMPANY_CATEGORY, true),
    },
    fieldName: 'category',
    label: '标签组类别',
  },
  {
    component: 'Input',
    fieldName: 'name',
    label: '标签组名称',
  },
  {
    component: 'Input',
    fieldName: 'labelName',
    label: '标签名称',
  },
  {
    component: 'Input',
    fieldName: 'remark',
    label: '备注',
  },
];

export const tagGroupColumns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60, resizable: false },
  {
    type: 'expand',
    width: 40,
    slots: { content: 'expandContent' },
    resizable: false,
  },
  {
    title: '标签组类别',
    field: 'category',
    slots: {
      default: ({ row }) => {
        // 区分企业级别1和个人级别2
        const typeDictKey =
          row.type === 1
            ? DictEnum.PERSONAL_TAG_COMPANY_CATEGORY
            : DictEnum.PERSONAL_TAG_PERSONAL_CATEGORY;
        return renderDict(row.category, typeDictKey);
      },
    },
  },
  {
    title: '标签组名称 (标签数量)',
    // titlePrefix: {
    //   content: '标签组里含多个标签',
    //   icon: 'vxe-table-icon-grouping',
    // },
    field: 'name',
    slots: {
      default: ({ row }) => {
        // icon={h(VbenIcon, { icon: 'streamline-flex:label-folder-tag' })}
        // return <Tag type="info">{row.name}</Tag>;
        return (
          <div>
            {row.name} ({row.labels?.length})
          </div>
        );
      },
    },
  },
  // {
  //   title: '标签数量',
  //   field: 'labels',
  //   slots: {
  //     default: ({ row }) => {
  //       return <span>{row.labels?.length}</span>;
  //     },
  //   },
  // },
  // {
  //   title: '类型',
  //   field: 'type',
  //   slots: {
  //     default: ({ row }) => {
  //       // return renderDict(row.status, DictEnum.SYS_COMMON_STATUS);
  //       return <span>{row.type === 1 ? '企业级别' : '个人级别'}</span>;
  //     },
  //   },
  // },
  {
    title: '备注',
    field: 'remark',
    showOverflow: true,
  },
  {
    title: '状态',
    field: 'status',
    slots: { default: 'status' },
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 280,
  },
];

export const tagGroupModalSchema: FormSchemaGetter = () => [
  {
    label: '标签组ID',
    fieldName: 'id',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '标签组类型',
    fieldName: 'type',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '标签组类别',
    fieldName: 'category',
    componentProps: {
      getPopupContainer,
      // options: getDictOptions(DictEnum.PERSONAL_TAG_COMPANY_CATEGORY, true),
      options: [], // 默认为空，动态查询企业、个人级别的标签组分类
    },
    component: 'Select',
    rules: 'selectRequired',
  },
  {
    label: '标签组名称',
    fieldName: 'name',
    componentProps: {
      showCount: true,
      maxlength: 20,
    },
    component: 'Input',
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
      optionType: 'button',
    },
    defaultValue: '0',
    fieldName: 'status',
    label: '状态',
    rules: 'selectRequired',
  },
  {
    component: 'Textarea',
    fieldName: 'remark',
    label: '备注',
    componentProps: {
      autoSize: { minRows: 3 },
    },
  },
];

export const tagGroupDetailsSchema: DetailsModalSchemaGetter = () => [
  {
    label: '标签组类别',
    fieldName: 'category',
    dictEnum: DictEnum.PERSONAL_TAG_COMPANY_CATEGORY,
  },
  {
    label: '标签组名称',
    fieldName: 'name',
    copy: true,
  },
  {
    label: '状态',
    fieldName: 'status',
    dictEnum: DictEnum.SYS_NORMAL_DISABLE,
  },
  {
    label: '标签',
    fieldName: 'labels',
    custom: true,
  },
  {
    label: '备注',
    fieldName: 'remark',
    textWrap: true,
  },
];

export const updateCategorySchema = (formApi: any, type: number | string) => {
  if (type === 1) {
    // 企业级别
    formApi.updateSchema([
      {
        fieldName: 'category',
        componentProps: {
          options: getDictOptions(DictEnum.PERSONAL_TAG_COMPANY_CATEGORY, true),
        },
      },
    ]);
  }
  if (type === 2) {
    //  个人级别
    formApi.updateSchema([
      {
        fieldName: 'category',
        componentProps: {
          options: getDictOptions(
            DictEnum.PERSONAL_TAG_PERSONAL_CATEGORY,
            true,
          ),
        },
      },
    ]);
  }
};

export const tagColumns: VxeGridProps['columns'] = [
  // {
  //   type: 'checkbox',
  //   width: 60,
  // },
  {
    dragSort: true,
    title: '拖拽排序',
    width: 100,
  },
  {
    type: 'seq',
    width: 60,
  },
  {
    field: 'name',
    title: '标签名称',
    align: 'left',
    minWidth: 300,
    slots: { default: 'name' },
  },
  {
    title: '备注',
    field: 'remark',
    showOverflow: true,
  },
  {
    title: '状态',
    field: 'status',
    slots: { default: 'status' },
    width: 80,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 200,
  },
];

export const tagModalSchema: FormSchemaGetter = () => [
  {
    label: '标签组ID',
    fieldName: 'groupId',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '标签ID',
    fieldName: 'id',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '标签类型',
    fieldName: 'type',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    label: '标签名称',
    fieldName: 'name',
    componentProps: {
      showCount: true,
      maxlength: 30,
    },
    component: 'Input',
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'listClass',
    label: '标签样式',
    defaultValue: 'primary',
  },
  {
    component: 'InputNumber',
    fieldName: 'sort',
    help: '排序, 数字越小越靠前',
    label: '显示排序',
    defaultValue: 0,
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    componentProps: {
      buttonStyle: 'solid',
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
      optionType: 'button',
    },
    defaultValue: '0',
    fieldName: 'status',
    label: '状态',
    rules: 'required',
  },
  {
    component: 'Textarea',
    fieldName: 'remark',
    label: '备注',
    componentProps: {
      autoSize: { minRows: 3 },
    },
  },
];

export const tagDetailsSchema: DetailsModalSchemaGetter = () => [
  {
    label: '标签名称',
    fieldName: 'name',
    copy: true,
  },
  {
    label: '状态',
    fieldName: 'status',
    dictEnum: DictEnum.SYS_NORMAL_DISABLE,
  },
  {
    label: '排序',
    fieldName: 'sort',
  },
  {
    label: '备注',
    fieldName: 'remark',
    textWrap: true,
  },
];
